﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace M1Xonix
{
    /// <summary>
    /// Klass, mis oskab joonistada ruudukest ja salvestada olekuparameetreid.
    /// </summary>
    internal class Square
    {
        private readonly int X;
        private readonly int Y;
        private readonly Color enemyColor = Color.White;
        private readonly Color hostileZoneColor = Color.Black;
        private readonly Color playerColor = Color.Yellow;
        private readonly Color safeZoneColor = Color.Blue;
        private readonly int squareSize;
        private readonly Texture2D squareTexture;
        private readonly Color temporaryColor = Color.Red;
        private bool enemyOccupied;
        private bool inSafeZone;
        private bool playerOccupied;
        private bool temporary;
        private bool tmpFill;

        /// <summary>
        /// Konstruktor, mis votab koik parameetrid
        /// </summary>
        /// <param name="locationX"></param>
        /// <param name="locationY"></param>
        /// <param name="squareTexture"></param>
        /// <param name="squareSize"></param>
        public Square(int locationX, int locationY, Texture2D squareTexture, int squareSize)
        {
            X = locationX;
            Y = locationY;
            this.squareTexture = squareTexture;
            this.squareSize = squareSize;
        }

        /// <summary>
        /// tagastab true, kui Square on määratud turvalises tsoonis asuvaks
        /// </summary>
        /// <returns></returns>
        public bool GetSafeZone()
        {
            return inSafeZone;
        }

        /// <summary>
        /// märgib Squarese turvalises tsoonis olevaks
        /// </summary>
        public void ToSafeZone()
        {
            inSafeZone = true;
        }

        /// <summary>
        /// märgib, et Square ei asu turvalises tsoonis 
        /// </summary>
        public void ClearSafeZone()
        {
            inSafeZone = false;
        }


        public void TogglePlayerOccupied()
        {
            playerOccupied = !playerOccupied;
        }

        /// <summary>
        /// märgib, et Square ei ole mängija poolt hõivatud
        /// </summary>
        public void ClearPlayerOccupied()
        {
            playerOccupied = false;
        }

        /// <summary>
        /// märgib, et Square on mängija poolt hõivatud
        /// </summary>
        public void SetPlayerOccupied()
        {
            playerOccupied = true;
        }

        /// <summary>
        /// tagastab true, kui Square on mängija saba all
        /// </summary>
        /// <returns></returns>
        public bool GetTemporary()
        {
            return temporary;
        }

        /// <summary>
        /// märgib Squarese mängija saba alla kuuluvaks
        /// </summary>
        public void ToTemporary()
        {
            temporary = true;
        }

        /// <summary>
        /// võtab Squareselt mängija saba märke maha
        /// </summary>
        public void ClearTemporary()
        {
            temporary = false;
        }

        public void ToggleEnemyOccupied()
        {
            enemyOccupied = !enemyOccupied;
        }

        /// <summary>
        /// tagastab true, kui Square on vaenlase poolt hõivatud
        /// </summary>
        /// <returns></returns>
        public bool GetEnemyOccupied()
        {
            return enemyOccupied;
        }

        /// <summary>
        /// märgib Squarese vaenlase poolt hõivatuks
        /// </summary>
        public void SetEnemyOccupied()
        {
            enemyOccupied = true;
        }

        /// <summary>
        /// märgib Squarese vaenlase poolt mitte hõivatuks
        /// </summary>
        public void ClearEnemyOccupied()
        {
            enemyOccupied = false;
        }

        /// <summary>
        /// tagastab true, kui Square on ajutiselt märgitud
        /// </summary>
        /// <returns></returns>
        public bool GetTmpFill()
        {
            return tmpFill;
        }

        /// <summary>
        /// lisab Squaresele asjutise märke
        /// </summary>
        public void SetTmpFill()
        {
            tmpFill = true;
        }

        /// <summary>
        /// võtab Squareselt ajutise märke maha
        /// </summary>
        public void ClearTmpFill()
        {
            tmpFill = false;
        }

        /// <summary>
        /// Joonistab ruudu ja värvib vastavalt olekule.
        /// </summary>
        /// <param name="spriteBatch"></param>
        public void DrawingMethod(SpriteBatch spriteBatch)
        {
            if (inSafeZone)
            {
                if (playerOccupied)
                    spriteBatch.Draw(squareTexture, new Rectangle(X, Y, squareSize, squareSize), playerColor);

                else
                    spriteBatch.Draw(squareTexture, new Rectangle(X, Y, squareSize, squareSize), safeZoneColor);
            }
            else
            {
                if (playerOccupied)
                    spriteBatch.Draw(squareTexture, new Rectangle(X, Y, squareSize, squareSize), playerColor);
                else
                {
                    if (temporary)
                        spriteBatch.Draw(squareTexture, new Rectangle(X, Y, squareSize, squareSize), temporaryColor);
                    else
                    {
                        if (enemyOccupied)
                            spriteBatch.Draw(squareTexture, new Rectangle(X, Y, squareSize, squareSize), enemyColor);
                        else
                            spriteBatch.Draw(squareTexture, new Rectangle(X, Y, squareSize, squareSize),
                                             hostileZoneColor);
                    }
                }
            }
        }
    }
}